//
//  MeCellNode.m
//  EducationLive
//
//  Created by Apple on 2017/10/17.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "MeCellNode.h"

@interface MeCellNode ()

// UI
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASImageNode *iconNode;
@property (nonatomic, strong) ASImageNode *arrowNode;
@property (nonatomic, strong) ASImageNode *lineNode;
// Data
@property (nonatomic, strong) MeModel *item;

@end

@implementation MeCellNode

- (instancetype)initWithItem:(MeModel *)item
{
    if (self = [super init]) {
        _item = item;
        self.backgroundColor = [UIColor whiteColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addTitleNode];
        
        [self addIconNode];
        
        [self addArrowNode];
        
        [self addLineNode];
    }
    return self;
}

- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(36, 36, 36)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:_item.iconName attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

- (void)addIconNode
{
    ASImageNode *iconNode = [[ASImageNode alloc]init];
    iconNode.layerBacked = YES;
    iconNode.backgroundColor = [UIColor clearColor];
    iconNode.image = [UIImage imageNamed:_item.iconImage];
    [self addSubnode:iconNode];
    _iconNode = iconNode;
}

- (void)addArrowNode
{
    ASImageNode *arrowNode = [[ASImageNode alloc]init];
    arrowNode.layerBacked = YES;
    arrowNode.backgroundColor = [UIColor clearColor];
    arrowNode.image = [UIImage imageNamed:@"xiala"];
    [self addSubnode:arrowNode];
    _arrowNode = arrowNode;
}

- (void)addLineNode
{
    ASImageNode *lineNode = [[ASImageNode alloc]init];
    lineNode.layerBacked = YES;
    lineNode.backgroundColor = RGB_255(230,230,230);
    [self addSubnode:lineNode];
    _lineNode = lineNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _arrowNode.style.preferredSize = CGSizeMake(9, 15);
    _iconNode.style.preferredSize = CGSizeMake(16, 16);
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, 0.5);
    ASStackLayoutSpec *titleLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_iconNode,_titleNode]];
    ASStackLayoutSpec *arrowLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[titleLayout,_arrowNode]];
    arrowLayout.style.flexGrow = YES;
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(14, 10, 14, 10) child:arrowLayout];
    
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[insetLayout,_lineNode]];

    return lineLayout;
}

@end
